package com.basic.business.operationStoreConfig.controller;


import java.util.List;
import java.io.IOException;

import com.basic.business.operationStoreConfig.domain.TOperationStoreConfig;
import com.basic.business.operationStoreConfig.service.ITOperationStoreConfigService;
import com.basic.business.store.vo.StoreSimpleDetailRespVo;
import com.basic.common.core.domain.entity.SysDept;
import com.basic.common.utils.SecurityUtils;
import com.basic.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;

import com.basic.common.annotation.FileExport;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.enums.BusinessType;


import com.basic.common.core.page.TableDataInfo;

/**
 * 运营店铺配置Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/operationStoreConfig")
public class TOperationStoreConfigController extends BaseController {
    @Resource
    private ITOperationStoreConfigService tOperationStoreConfigService;

    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 查询运营店铺配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:operationStoreConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(TOperationStoreConfig tOperationStoreConfig) {
        startPage();
        List<TOperationStoreConfig> list = tOperationStoreConfigService.getList(tOperationStoreConfig);
        return getDataTable(list);
    }

    /**
     * 导出运营店铺配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:operationStoreConfig:export')")
    @Log(title = "运营店铺配置", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "运营店铺配置.xlsx")
    @PostMapping("/export")
    public String export(TOperationStoreConfig tOperationStoreConfig) throws IOException {
        List<TOperationStoreConfig> list = tOperationStoreConfigService.getList(tOperationStoreConfig);
        return EasyExcelUtils.excelExport(TOperationStoreConfig.class, "运营店铺配置数据", list);
    }

    /**
     * 获取运营店铺配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:operationStoreConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tOperationStoreConfigService.get(id));
    }

    /**
     * 新增运营店铺配置
     */
    @PreAuthorize("@ss.hasPermi('business:operationStoreConfig:add')")
    @Log(title = "运营店铺配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOperationStoreConfig tOperationStoreConfig) {
        return toAjax(tOperationStoreConfigService.insert(tOperationStoreConfig));
    }

    /**
     * 修改运营店铺配置
     */
    @PreAuthorize("@ss.hasPermi('business:operationStoreConfig:edit')")
    @Log(title = "运营店铺配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOperationStoreConfig tOperationStoreConfig) {
        return toAjax(tOperationStoreConfigService.update(tOperationStoreConfig));
    }

    /**
     * 删除运营店铺配置
     */
    @PreAuthorize("@ss.hasPermi('business:operationStoreConfig:remove')")
    @Log(title = "运营店铺配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tOperationStoreConfigService.deleteByIds(ids));
    }

    /**
     * 根据类型获取店铺配置
     * @param configType
     * @return
     */
    @GetMapping("/getConfigByType")
    public AjaxResult getConfigByType(Integer configType) {
        Long deptId = SecurityUtils.getDeptId();

        SysDept sysDept = sysDeptService.selectDeptById(deptId);

        TOperationStoreConfig config = tOperationStoreConfigService.getConfigByTypeAndAncestors(configType, deptId, sysDept.getAncestors());

        return success(config);

    }

    /**
     * 根据类型获取店铺配置详情
     * @param configType
     * @return
     */
    @GetMapping("/getConfigDetailByType")
    public AjaxResult getConfigDetailByType(Integer configType) {
        Long deptId = SecurityUtils.getDeptId();

        SysDept sysDept = sysDeptService.selectDeptById(deptId);

        List<StoreSimpleDetailRespVo> list = tOperationStoreConfigService.getStoreSimpleDetailList(configType, deptId, sysDept.getAncestors());

        return success(list);

    }
}
